﻿<!--
* 开源版本请务必保留此注释头信息，若删除捷码开源〔GEMOS〕官方保留所有法律责任追究！
* 本软件受国家版权局知识产权以及国家计算机软件著作权保护（登记号：2018SR503328）
* 不得恶意分享产品源代码、二次转售等，违者必究。
* Copyright (c) 2020 gemframework all rights reserved.
* http://www.gemframework.com
* 版权所有，侵权必究！
-->
<!DOCTYPE html>
<html lang="zh-cn" class="fullscreen-bg" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:replace="common/links :: common_header(~{::title},~{},~{})">
    <title>定时任务-模块</title>
</head>
<body>
<!-- 页面加载loading -->
<div class="page-loading">
    <div class="ball-loader">
        <span></span><span></span><span></span><span></span>
    </div>
</div>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格工具栏 -->
            <form class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">任务名称:</label>
                        <div class="layui-input-inline">
                            <input name="jobName" class="layui-input" placeholder="请输入任务名称"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">创建时间:</label>
                        <div class="layui-input-inline">
                            <input name="createDateRange" class="layui-input icon-date" placeholder="选择日期范围"
                                   autocomplete="off"/>
                        </div>
                    </div>
                    <div class="layui-inline">&emsp;
                        <button class="layui-btn icon-btn" lay-filter="searchBtn" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>&nbsp;
                        <button shiro:hasPermission="jobs:export" id="exportBtn" class="layui-btn icon-btn"
                                type="button">
                            <i class="layui-icon">&#xe67d;</i>导出
                        </button>
                    </div>
                </div>
            </form>
            <!-- 数据表格 -->
            <table id="dataTable" lay-filter="dataTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="tableBar">
    <a shiro:hasPermission="jobs:update" class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    {{#if(d.jobType=== 1){ }}
    <a shiro:hasPermission="jobs:exec" class="layui-btn layui-btn-xs" lay-event="exec">手动执行</a>
    {{# }else if(d.jobType=== 2){ }}
    <a shiro:hasPermission="jobs:exec" class="layui-btn layui-btn-xs" lay-event="exec">自动执行</a>
    {{# } }}
    <a shiro:hasPermission="jobs:stop" class="layui-btn layui-btn-warm layui-btn-xs" lay-event="stop">暂停</a>
    <a shiro:hasPermission="jobs:delete" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="formDialog">
    <form id="dataForm" lay-filter="dataForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">任务名称:</label>
            <div class="layui-input-block">
                <input id="jobName" name="jobName"
                       placeholder="请输入任务名称" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
            <input id="jobGroup" name="jobGroup" type="hidden" />
            <input id="triggerName" name="triggerName" type="hidden" />
            <input id="triggerGroup" name="triggerGroup" type="hidden" />
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">执行类名:</label>
            <div class="layui-input-block">
                <input name="executeClassName" placeholder="请输入Spring Bean名" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">任务类型</label>
            <div class="layui-input-block">
                <select name="jobType" lay-search onchange="showView(this.value)">
                    <option value="1" selected>简单任务</option>
                    <option value="2">Cron任务</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item trigger_cron" style="display: none">
            <label class="layui-form-label layui-form-required">表达式:</label>
            <div class="layui-input-block">
                <input name="triggerCronExpression" placeholder="请输入表达式"
                       class="layui-input"lay-verType="tips"/>
            </div>
        </div>
        <div class="trigger_simple">
            <div class="layui-form-item">
                <label class="layui-form-label layui-form-required">启动时间:</label>
                <div class="layui-input-block">
                    <input id="triggerStartTime" name="triggerStartTime" placeholder="请选择起始时间"
                           class="layui-input icon-date" autocomplete="off" lay-verType="tips"/>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">任务参数:</label>
            <div class="layui-input-block">
                <textarea name="jobParams" placeholder="请输入任务参数" class="layui-textarea" maxlength="200"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">任务描述</label>
            <div class="layui-input-block">
                <textarea name="remark" placeholder="请输入内容" class="layui-textarea" maxlength="200"></textarea>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="saveBtn" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>
<!-- 表格状态列 -->
<script type="text/html" id="status">
    <p>{{d.status==0?'<font color="red">暂停</font>':'<font color="green">运行中...</font>'}}</p>
</script>
<!-- js部分 -->
<div th:replace="common/scripts :: common"></div>
<div th:replace="common/scripts :: jquery"></div>
<div th:replace="common/scripts :: custom"></div>
<div>
    <script>
        //全局变量 Shiro权限
        var shiroSave = false;
        var shiroDelete = false;
        var shiroUpdate = false;
        var shiroInfo = false;
    </script>
    <!-- 设置Shiro权限变量 -->
    <shiro:hasAllPermissions name="jobs:save">
        <script>shiroSave = true;</script>
    </shiro:hasAllPermissions>
    <shiro:hasAllPermissions name="jobs:delete">
        <script>shiroDelete = true;</script>
    </shiro:hasAllPermissions>
    <shiro:hasAllPermissions name="jobs:update">
        <script>shiroUpdate = true;</script>
    </shiro:hasAllPermissions>
</div>
<script th:inline="javascript">
    layui.use(['layer', 'form', 'table', 'util', 'laydate', 'tableX', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var laydate = layui.laydate;
        var tableX = layui.tableX;
        var admin = layui.admin;

        /* 渲染表格 */
        var cols = [
            {type: 'checkbox'},
            {field: 'jobName', title: '任务名称', align: 'center'},
            {field: 'jobType', title: '任务类型',  templet: function (d) {
                    if(d.jobType === 1){
                        return "简单任务"
                    }else if(d.jobType === 2){
                        return "Cron任务"
                    }else{
                        return "未定义"
                    }},width:100, align: 'center'},
            {field: 'triggerCronExpression', title: '表达式', align: 'center'},
            {field: 'executeClassName', title: '执行类名', align: 'center'},
            {field: 'status', title: '状态', templet: function (d) {
                    if(d.status === 1){
                        return "<font color=\"green\">运行中...</font>";
                    }else if(d.status === 0){
                        return "<font color=\"red\">暂停</font>";
                    }else{
                        return "<font color=\"gray\">未知状态</font>";
                    }}, align: 'center'},
            // {
            //     field: 'updateTime', title: '创建时间', templet: function (d) {
            //         return util.toDateString(d.updateTime);
            //     }, sort: true,minWidth:180,align: 'center'
            // },
            {
                field: 'remark', title: '任务描述'
            },
            {title: '操作', toolbar: '#tableBar', align: 'center', minWidth: 240}
        ];

        var insTb = table.render({
            elem: '#dataTable',
            url: 'jobs/page',
            page: true,
            toolbar: [shiroToolbar(shiroSave, shiroDelete)].join(''),
            cellMinWidth: 100,
            cols: [cols],
            done: function () {
                // 绑定鼠标右键
                tableX.bindCtxMenu('dataTable', shiroBindCtxMenu(shiroDelete, shiroUpdate));
            }
        });

        /* 表格工具条点击事件 */
        table.on('tool(dataTable)', function (obj) {
            if (obj.event === 'edit') { // 修改
                showEditModel(obj.data);
            } else if (obj.event === 'del') { // 删除
                if ([[${session.session_runtime}]] == 'demo') return layer.msg('演示环境不允许操作，请联系管理员', {icon: 4});
                doDel(obj.data);
            } else if (obj.event === 'exec') { // 执行
                if(obj.data.jobType === 2 && obj.data.status == 1){
                    layer.msg("任务运行中!", {icon: 2,time: 20000});
                    return false;
                }
                $.get("jobs/exec", obj.data, function (res) {
                    if (res.code == 0) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.reload({page: {curr: 1}});
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'json');
            } else if (obj.event === 'stop') { // 停止
                $.get("jobs/stop", obj.data, function (res) {
                    if (res.code == 0) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.reload({page: {curr: 1}});
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'json');
            }
        });

        /* 表格头工具栏点击事件 */
        table.on('toolbar(dataTable)', function (obj) {
            if (obj.event === 'add') { // 添加
                showEditModel();
            } else if (obj.event === 'del') { // 删除
                var checkRows = table.checkStatus('dataTable');
                if (checkRows.data.length === 0) {
                    layer.msg('请选择要删除的数据', {icon: 2});
                    return;
                }
                var ids = checkRows.data.map(function (d) {
                    return d.id;
                });
                doDel({ids: ids});
            }
        });

        // 显示编辑弹窗
        function showEditModel(mData) {
            admin.open({
                type: 1,
                area: '800px',
                title: (mData ? '修改' : '添加'),
                content: $('#formDialog').html(),
                shadeClose:true,
                success: function (layero, dIndex) {
                    form.val('dataForm', mData);  // 回显数据
                    // 回显时渲染菜单和按钮的显示
                    showView(mData?(mData.jobType?mData.jobType:1):1);

                    //输入框的值改变时触发
                    $("#jobName").on("input",function(e){
                        //获取input输入的值
                        $("#jobGroup").val(e.delegateTarget.value);
                        $("#triggerName").val(e.delegateTarget.value);
                        $("#triggerGroup").val(e.delegateTarget.value);
                    });

                    form.on('submit(saveBtn)', function (data) {
                        $.post(mData ? 'jobs/update' : 'jobs/save', data.field, function (res) {
                            if (res.code == 0) {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                                insTb.reload({page: {curr: 1}});
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'json');
                        return false;
                    });

                    /* 渲染时间选择 */
                    laydate.render({
                        elem: 'input[name="triggerStartTime"]',
                        type: 'datetime',
                        trigger: 'click'
                    });
                    laydate.render({
                        elem: 'input[name="triggerStopTime"]',
                        type: 'datetime',
                        trigger: 'click'
                    });
                }
            });

        }

        /* 导出excel */
        $('#exportBtn').click(function () {
            var checkRows = table.checkStatus('dataTable');
            if (checkRows.data.length === 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                tableX.exportDataX({
                    cols: insTb.config.cols,
                    data: checkRows.data,
                    fileName: '导出数据'
                });
            }
        });


        /* 删除 */
        window.doDel = function (obj) {
            layer.confirm('确定要删除选中数据吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                $.post('jobs/delete', {
                    id: obj ? obj.id : '',
                    ids: obj.ids ? obj.ids.join(',') : ''
                }, function (res) {
                    layer.close(loadIndex);
                    if (res.code === 0) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.reload({page: {curr: 1}});
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'json');
            });
        }

        /* 渲染时间选择 */
        laydate.render({
            elem: 'input[name="createDateRange"]',
            type: 'date',
            range: true,
            trigger: 'click'
        });
        laydate.render({
            elem: 'input[name="updateDateRange"]',
            type: 'date',
            range: true,
            trigger: 'click'
        });
        laydate.render({
            elem: 'input[name="createTimeRange"]',
            type: 'date',
            range: true,
            trigger: 'click'
        });
        laydate.render({
            elem: 'input[name="updateTimeRange"]',
            type: 'date',
            range: true,
            trigger: 'click'
        });
        /* 表格搜索 */
        form.on('submit(searchBtn)', function (data) {
            if(data.field.updateDateRange) {
                var searchDate = data.field.updateDateRange.split(' - ');
                data.field.updateStartDate = searchDate[0];
                data.field.updateEndDate = searchDate[1];
            }else{
                data.field.updateStartDate = null;
                data.field.updateEndDate = null;
            }
            if(data.field.createDateRange) {
                var searchDate = data.field.createDateRange.split(' - ');
                data.field.createStartDate = searchDate[0];
                data.field.createEndDate = searchDate[1];
            }else{
                data.field.createStartDate = null;
                data.field.createEndDate = null;
            }
            if(data.field.updateTimeRange) {
                var searchDate = data.field.updateDateRange.split(' - ');
                data.field.updateStartTime = searchDate[0];
                data.field.updateEndTime = searchDate[1];
            }else{
                data.field.updateStartTime = null;
                data.field.updateEndTime = null;
            }
            if(data.field.createTimeRange) {
                var searchDate = data.field.createDateRange.split(' - ');
                data.field.createStartTime = searchDate[0];
                data.field.createEndTime = searchDate[1];
            }else{
                data.field.createStartTime = null;
                data.field.createEndTime = null;
            }
            data.field.updateDateRange = undefined;
            data.field.createDateRange = undefined;
            data.field.updateTimeRange = undefined;
            data.field.createTimeRange = undefined;
            insTb.reload({where: data.field, page: {curr: 1}});
            return false;
        });

    });
    function showView(type) {
        if(type == 1){
            $(".trigger_simple").show();
            $(".trigger_cron").hide();
        }else{
            $(".trigger_simple").hide();
            $(".trigger_cron").show();
        }
    }
</script>
</body>
</html>